package org.bao.action;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.bao.bean.usermessageBean;
import org.bao.service.ILoginService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.http.HttpRequest;
import com.sun.xml.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

/**
 * @ClassName: userLoginAction
 * @Description: 登陆actiuon
 * @author bql
 * @date 2019年4月27日
 *
 */
@Controller
public class UserLoginAction {

	/*
	 * springmvc框架中的ajax影响处理： 响应方法添加@ResponseBody注解； 响应方法的返回值为集合类型； 解析：
	 * 注解ResponseBody会使springmvc进行ajax的response响应处理；【
	 * 类似于servlet中的使用printwriter写出json字符串】；
	 * 方法返回值类型为集合类型，springmvc框架会自动将该集合类型转换为json字符串响应给客户端；【需要在springmvc配置文件中进行配置】
	 */

	// 依赖注入【反转控制】
	// 面向接口的编程
	@Resource
	private ILoginService loginS;// 按名称匹配进行注入

	/*
	 * 接受电话和密码去数据库中查找用户
	 */
	@RequestMapping("/LoginAction")
	public ResponseEntity<String> delogin(usermessageBean userB,HttpServletRequest request) {

		String usernm = null;
		Integer userid = null;
		
		// 打印电话与密码
		System.out.println("电话:" + userB.getUsertel());
		System.out.println("密码:" + userB.getUserpsw());

		List<usermessageBean> lisuser = loginS.queryLoginInfo(userB);
		
		//遍历list
		for(usermessageBean user : lisuser) {
			usernm = user.getUsername();
			userid = user.getUserid();
		}
		
		System.out.println("用户名:"+usernm);
		System.out.println("用户编号:"+userid);
		//缓存登录账户的个人信息
		request.getSession().setAttribute("userTel", userB.getUsertel());
		request.getSession().setAttribute("userName", usernm);
		request.getSession().setAttribute("userid", userid);
		request.getSession().setAttribute("userPsw", userB.getUserpsw());
		
		//判断是否成功
		if (lisuser != null && lisuser.size() > 0) {
//			查询成功,到主界面
			return ResponseEntity.status(HttpStatus.OK).body("1");
		} else {
			return ResponseEntity.status(HttpStatus.OK).body("0");
		}

	}

	/**
	 * @Title: delogin2
	 * @Description: 接受电话去数据库中登记此用户
	 * @param userB
	 * @return
	 * @return ResponseEntity<String> 返回类型
	 */
	@RequestMapping("/LoginAction2")
	public ResponseEntity<String> delogin2(HttpServletRequest request) {
		int count = 0;

		// 打印电话与验证码
		System.out.println("电话:" + request.getParameter("usertel"));
		System.out.println("验证码:" + request.getParameter("authmsg"));

		usermessageBean userB = new usermessageBean();
		userB.setUsertel(request.getParameter("usertel"));
		userB.setAuthmsg(request.getParameter("authmsg"));
		
		List<usermessageBean> lisuser = loginS.queryLoginInfo2(userB);

		Object[] arrlis = lisuser.toArray();
		
		for (Object userBB : arrlis) {
			userB = (usermessageBean) userBB;
		}
		
		request.getSession().setAttribute("userTel", userB.getUsertel());
		request.getSession().setAttribute("userName", userB.getUsername());
		request.getSession().setAttribute("userid", userB.getUserid());
		request.getSession().setAttribute("userPsw", userB.getUserpsw());
		
		// 如果查不到该用户则插入改用户,1插入结果，2查询到用户
		if (lisuser == null || lisuser.size() < 1) {
			count = loginS.insertTelAndAuthmsg(userB);
			return ResponseEntity.status(HttpStatus.OK).body("1");
		} else {
			return ResponseEntity.status(HttpStatus.OK).body("2");
		}

	}

}
